package com.rohoo.user.action;

import java.net.URLDecoder;

import org.apache.commons.codec.digest.DigestUtils;

import com.rohoo.framework.struts.action.BaseAction;
import com.rohoo.user.bo.LoginUser;
import com.rohoo.user.service.SloginService;

public class SloginAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private SloginService sloginService;
	
	//用户ID
	private int userid;
	//登录名称
	private String loginname;
	//用户名
	private String realname;
	//密码
	private String loginpwd;
	//登录界面类型
	private String type;
	
	//错误类型
	private String error;
	
	public String login() throws Exception {
		LoginUser loginUser = new LoginUser();
		loginname=URLDecoder.decode(loginname, "utf-8");
		loginpwd=URLDecoder.decode(loginpwd, "utf-8");
		userid = sloginService.getUserId(loginname);
		if(getSession().getAttribute("loginuser")!=null) {
			getSession().removeAttribute("loginuser");
		}
		if(userid!=0) {
			if(DigestUtils.md5Hex(loginpwd).equals(sloginService.getPwd(loginname))) {
				loginUser = sloginService.getLoginUser(userid);
				getSession().setAttribute("loginuser",loginUser);
				this.setError("0");
				return SUCCESS;
			}
			else {
				this.setError("2");//用户密码输入错误
				return "error";
			}
		}
		else {
			this.setError("1");//没有该用户
			return "error";
		}
	}
	
	public String loginff() {
		getSession().removeAttribute("loginuser");
		getSession().invalidate();
		getRequest().getSession(false);
		return SUCCESS;
	}
	
	public LoginUser getLoginUser(int userid) throws Exception {
		LoginUser loginUser = new LoginUser();
		if(userid!=0) {
			loginUser = sloginService.getLoginUser(userid);
		}
		return loginUser;
	}
	
	public SloginService getSloginService() {
		return sloginService;
	}
	public void setSloginService(SloginService sloginService) {
		this.sloginService = sloginService;
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getLoginpwd() {
		return loginpwd;
	}

	public void setLoginpwd(String loginpwd) {
		this.loginpwd = loginpwd;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
}
